package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.entity.ShopCart;
import com.example.demo.entity.ShopCartInformation;
import com.example.demo.service.ShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@Api(tags = "购物车商品详情管理",value = "购物车商品管理")
@RestController
@RequestMapping("/shop")
public class ShopAddProductController {
    @Autowired
    ShopCartService cartService;

    @PostMapping("/addProduct")
    @ApiOperation(value = "用户购物车添加商品",httpMethod = "POST")
    public Result<Integer> ShopAddProduct(@RequestBody ShopCartInformation shopCartInformation){
        Integer T;
        ShopCart shopCart=cartService.UserShopCartId(shopCartInformation.getItemUserId());
        if(shopCart!=null){
            ShopCartInformation shopCartInformation1=cartService.SelectShopItem(shopCartInformation.getItemUserId(),shopCartInformation.getItemSkuId());
            if(shopCartInformation1!=null){
                shopCartInformation.setItemSkuCount(shopCartInformation1.getItemSkuCount()+shopCartInformation.getItemSkuCount());
                System.out.println(shopCartInformation.getItemSkuCount());
                Integer integer= cartService.AddUserProductCount(shopCartInformation);
                return Result.success("已经有该商品",integer);
            }
            shopCartInformation.setItemCartId(new BigDecimal(shopCart.getCartId()));
            Integer i=cartService.ShopAddProduct(shopCartInformation);
            return Result.success("成功添加到购物车",i);
        }else {
            Integer t=cartService.InSertUserCartId(shopCartInformation.getItemUserId());
            ShopCart shopCart1=cartService.UserShopCartId(shopCartInformation.getItemUserId());
            shopCartInformation.setItemCartId(new BigDecimal(shopCart1.getCartId()));
            T= cartService.ShopAddProduct(shopCartInformation);
        }
        return Result.success("成功添加到购物车",T);
    }
}
